//
//  STDSProvider.h
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/15.
//

#ifndef STDSProvider_h
#define STDSProvider_h
#include <stdio.h>

#ifdef __ANDROID__
#include <jni.h>

struct STDRenderEngineProvider {
    void (*getImageFunc)(JNIEnv *env,  jobject provider, const char *path, unsigned char **data, int *widht, int *height);
    void (*getFontPath)(JNIEnv *env, jobject provider, const char *name, char **path);
};
#elif defined(__APPLE__)
struct STDRenderEngineProvider {
    void (*getImageFunc)(const char *path, unsigned char **data, int *widht, int *height);
    void (*getFontPath)(const char *name, char **path);
};
#endif

class STDSProvider {
public:
#ifdef __ANDROID__
    STDSProvider(JNIEnv *env, jobject provider, STDRenderEngineProvider *funcs) {
        this->env = env;
        this->provider = provider;
        this->funcs = funcs;
    }
    ~STDSProvider() {
        env->DeleteGlobalRef(provider);
        delete funcs;
    }
#elif defined(__APPLE__)
    STDSProvider(STDRenderEngineProvider *provider) {
        funcs = provider;
    }
    ~STDSProvider() {
//        delete funcs;
    }
#endif

    void getImageFunc(const char *path, unsigned char **data, int *widht, int *height) {
#ifdef __ANDROID__
        funcs->getImageFunc(env, provider, path, data, widht, height);
#elif defined(__APPLE__)
        funcs->getImageFunc(path, data, widht, height);
#endif
    }

    void getFontPath(const char *name, char **path) {
#ifdef __ANDROID__
        funcs->getFontPath(env, provider, name, path);
#elif defined(__APPLE__)
        funcs->getFontPath(name, path);
#endif
    }

public:
#ifdef __ANDROID__
    JNIEnv *env{nullptr};
    jobject provider{nullptr};
    STDRenderEngineProvider *funcs{nullptr};
#elif defined(__APPLE__)
    STDRenderEngineProvider *funcs{nullptr};
#endif
};

#endif /* STDSProvider_h */
